package com.jlu.easyexcel.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.jlu.easyexcel.domain.Student;
import com.jlu.easyexcel.listener.WebStudentListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/student")
public class StudentController {
  
  @Autowired
  WebStudentListener webStudentListener;

  @PostMapping("read")
  public String read(MultipartFile multipartFile){

    try {

      ExcelReaderBuilder readWorkBook = EasyExcel.read(multipartFile.getInputStream(), Student.class, webStudentListener);

      readWorkBook.sheet().doRead();
      return "success";
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "fail";
  }
}
